#include "camera.h"
#include <d3dx9.h>
#include <math.h>
using namespace ns;

Camera::Camera(Renderer* _renderer){
	renderer = _renderer;
	pitch = yaw = 0;
	x = y = z = lx = ly = 0;
	lz = 1;
}

Camera::~Camera(){
	delete renderer;
	renderer = NULL;
}
//movimiento respectivo a la cam
//movimiento frontal
void Camera::Walk(float ammount){
	float wx = 0;
	float wz = ammount;
	
	wx = (wx*cos(yaw))+(wz*sin(yaw));
	wz = (-wx*sin(yaw))+(wz*cos(yaw));

	x+=wx;
	z+=wz;
}

//movimiento horizontal
void Camera::Strafe(float ammount){
	float wx = ammount;
	float wz = 0;
	
	wx = (wx*cos(yaw))+(wz*sin(yaw));
	wz = (-wx*sin(yaw))+(wz*cos(yaw));

	x+=wx;
	z+=wz;
}

void Camera::Jump(float ammount){
	y += ammount;
}

void Camera::UpdateCam(){
	lx = ly = 0;
	lz = 1;

	ly = (ly*cos(pitch))-(lz*sin(pitch));
	lz = (ly*sin(pitch))+(lz*cos(pitch));

	
	lx = (lx*cos(yaw))+(lz*sin(yaw));
	lz = (-lx*sin(yaw))+(lz*cos(yaw));

	lx += x;
	ly += y;
	lz += z;
	renderer->UpdateView(x,y,z,lx,ly,lz);

}
//x
void Camera::Pitch(float angle){
	if(pitch+angle>0.8||pitch+angle<-0.8)
		return;
	cout<<pitch;
	pitch += angle;
}
//y
void Camera::Yaw(float angle){
	yaw += angle;
	if(yaw>360) yaw-=360;
	if(yaw<-360) yaw+=360;
}
//z
void Camera::Roll(float angle){
	lx = (lx*cos(angle))-(ly*sin(angle));
	ly = (lx*sin(angle))+(ly*cos(angle));
}